package com.qingyun.form.base

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.core.content.withStyledAttributes
import androidx.recyclerview.widget.RecyclerView
import androidx.core.graphics.drawable.toDrawable
import androidx.core.graphics.withSave
import kotlin.math.max
import kotlin.math.min

class FormItemDecoration(
    context: Context,
    @ColorInt color: Int = -1,
) : RecyclerView.ItemDecoration() {
    companion object {
        val ATTRS: IntArray = intArrayOf(android.R.attr.listDivider)
    }

    private lateinit var drawable: Drawable
    private val bounds = Rect()
    var paddingTop: Int = -1
    var paddingBottom: Int = -1
    var height = -1
    private val dividerHeight: Int
        get() = max(height, drawable.intrinsicHeight)

    init {
        if (color == -1) {
            context.withStyledAttributes(null, ATTRS) {
                drawable = getDrawable(0) ?: ColorDrawable()
            }
        } else {
            drawable = color.toDrawable()
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (parent.layoutManager == null) {
            return
        }
        c.withSave {
            val left: Int
            val right: Int
            if (parent.clipToPadding) {
                left = parent.paddingLeft
                right = parent.width - parent.paddingRight
                c.clipRect(left, parent.paddingTop, right, parent.height - parent.paddingBottom)
            } else {
                left = 0
                right = parent.width
            }

            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)
                parent.getDecoratedBoundsWithMargins(child, bounds)
                val holder = parent.getChildViewHolder(child) as FormViewHolder
                var itemDividerHeight = holder.getDividerHeight()
                if (itemDividerHeight == -1) {
                    itemDividerHeight = dividerHeight
                }
                var bottom = bounds.bottom + child.translationY.toInt()
                if (i == childCount - 1 && paddingBottom > 0) {
                    bottom -= paddingBottom
                }
                val top = bottom - itemDividerHeight
                drawable.setBounds(left, top, right, bottom)
                drawable.draw(c)
            }

        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State,
    ) {
        var top: Int = 0
        var bottom: Int = dividerHeight
        val position = (view.layoutParams as RecyclerView.LayoutParams).viewAdapterPosition
        if (position == 0 && paddingTop != -1) {
            top = paddingTop
        }
        if (position == state.itemCount - 1 && paddingBottom != -1) {
            bottom += paddingBottom
        }
        outRect.set(0, top, 0, bottom)
    }

}